<script setup lang="ts">
import { ref, nextTick } from 'vue'
import type { Ref } from 'vue'
import {
  CircleCheck,
  Delete,
  Edit,
  User,
  DArrowRight,
  Search,
  Refresh,
  Plus,
  Download
} from '@element-plus/icons-vue'
// 引入组件
import Pagination from '@/components/Pagination/Index.vue'
import { ElMessage, ElMessageBox } from 'element-plus'

import { usersList } from '@/api/admin/system/user'

// queryForm ref
const queryFormRef: Ref<null> = ref(null)
/**查询条件 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  userName: undefined,
  phonenumber: undefined,
  status: undefined
})
/** 日期查询条件 */
const dateRange = ref([])
const sys_normal_disable = ref([
  { label: '正常', value: '0' },
  { label: '停用', value: '1' }
])
/**表格的loading */
const loading = ref(false)
/**表格数据 */
const userList: any = ref([])
/**总共条数 */
const total = ref(0)

// 查询用户列表
const getList = () => {
  console.log('查询用户', queryParams, dateRange)
  // usersList(queryParams).then((res:any) => {
  //   console.log('查询用户列表', res)
  // })
  loading.value = true
  interface res {
    code: number
    rows: {
      userId: string
      userName: string
      nickName: string
      loginName: string
      phonenumber: string
      status: number
      createTime: string
    }[]
    total: number
  }
  const response: res = {
    code: 200,
    rows: [
      {
        userId: '1000001',
        userName: '张学友',
        nickName: '歌神',
        loginName: '18552126789',
        phonenumber: '18552126789',
        status: 0,
        createTime: '2024-05-13 18:00:40'
      },
      {
        userId: '1000002',
        userName: '刘德华',
        nickName: '赌侠',
        loginName: '18888888888',
        phonenumber: '18888888888',
        status: 1,
        createTime: '2024-05-14 18:00:40'
      },
      {
        userId: '1000003',
        userName: '周润发',
        nickName: '赌圣',
        loginName: '16666666666',
        phonenumber: '16666666666',
        status: 1,
        createTime: '2024-05-14 18:00:40'
      }
    ],
    total: 11
  }
  userList.value = response.rows
  total.value = response.total
  loading.value = false
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1
  getList()
}
/**重置数据 */
const resetQuery = () => {
  dateRange.value = []
  queryFormRef?.value.resetFields()
  handleQuery()
}

/**
 * 非单个禁用
 */
const single = ref(true)
/**
 * 选中多个进行删除
 */
const multiple = ref(true)
/**选中删除的id */
const ids = ref([])

/**
 * 删除
 */
const handleDelete = (row: any) => {
  const userIds = row.userId || ids.value
  ElMessageBox.confirm('是否确认删除用户编号为"' + userIds + '"的数据项?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(function () {
      // return delUser(userIds);
      console.log('删除用户')
    })
    .then(() => {
      getList()
      ElMessage.success('删除成功')
    })
    .catch(() => {})
}
/**
 * 用户状态修改
 */
const handleStatusChange = (row: any) => {
  const text = row.status === '0' ? '启用' : '停用'
  ElMessageBox.confirm('确认要"' + text + '""' + row.userName + '"用户吗?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(function () {
      // return changeUserStatus(row.userId, row.status);
      console.log('删除')
    })
    .then(() => {
      ElMessage.success(text + '成功')
    })

    .catch(function () {
      row.status = row.status === '0' ? '1' : '0'
    })
}

/**
 * 选择条数
 */
const handleSelectionChange = (selection: any) => {
  ids.value = selection.map((item: any) => item?.userId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/**
 * 修改
 */
const handleUpdate = (row: any) => {
  console.log('修改===', row)
  //  resetOperForm();
  //  initTreeData();
  //  const userId = row.userId || ids.value;
  //  // 获取用户的岗位,角色等信息
  //  getUser(userId).then(response => {
  //     operForm.value.form = response.data;
  //     operForm.value.postOptions = response.posts;
  //     operForm.value.roleOptions = response.roles;
  //     operForm.value.form.postIds = response.postIds;
  //     operForm.value.form.roleIds = response.roleIds;
  //     operForm.value.open = true;
  //     operForm.value.title = "修改用户";
  //     operForm.value.form.password = "";
  //  });
}

/** 重置密码按钮操作 */
const handleResetPwd = (row: any) => {
  ElMessageBox.prompt('请输入"' + row.userName + '"的新密码', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消'
  })
    .then(({ value }) => {
      console.log('重置密码', value)
      // resetUserPwd(row.userId, value).then(response => {
      //    ElMessage.success("修改成功，新密码是：" + value);
      // });
    })
    .catch(() => {})
}

// 初始化列表数据
getList()
</script>

<template>
  <div class="user">
    <!-- 条件查询 -->
    <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="用户状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
        <el-button :icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 按钮组 -->
    <el-row :gutter="10" style="margin-bottom: 10px">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:role:add']"
          type="primary"
          plain
          :icon="Plus"
          @click="handleAdd"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:role:edit']"
          type="success"
          plain
          :icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:role:remove']"
          type="danger"
          plain
          :icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:role:export']"
          type="warning"
          plain
          :icon="Download"
          @click="handleExport"
        >
          导出</el-button
        >
      </el-col>
    </el-row>
    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column key="userId" label="用户编号" align="center" prop="userId" />
      <el-table-column
        key="userName"
        label="用户名称"
        align="center"
        prop="userName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="nickName"
        label="用户昵称"
        align="center"
        prop="nickName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        key="phonenumber"
        label="手机号码"
        align="center"
        prop="phonenumber"
        width="120"
      />
      <el-table-column key="status" label="状态" align="center">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="0"
            :inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <!-- <span>{{ parseTime(scope.row.createTime) }}</span> -->
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="250"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            v-if="scope.row.userId !== 1"
            v-hasPermi="['system:user:edit']"
            type="primary"
            link
            @click="handleUpdate(scope.row)"
          >
            <el-icon> <edit /> </el-icon>修改
          </el-button>
          <el-button
            v-if="scope.row.userId !== 1"
            v-hasPermi="['system:user:remove']"
            type="primary"
            link
            @click="handleDelete(scope.row)"
          >
            <el-icon> <delete /> </el-icon>删除
          </el-button>
          <el-button
            v-if="scope.row.userId !== 1"
            v-hasPermi="['system:user:edit']"
            type="primary"
            link
            @click="handleResetPwd(scope.row)"
          >
            <el-icon> <key /> </el-icon>重置密码
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>

<style scoped lang="scss"></style>
